<template>
	<div class="sys-menu-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="769px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ state.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级菜单">
							<el-cascader
								:options="state.menuData"
								:props="{ checkStrictly: true, emitPath: false, value: 'id', label: 'title' }"
								placeholder="请选择上级菜单"
								clearable
								class="w100"
								v-model="state.ruleForm.parentId"
							>
								<template #default="{ node, data }">
									<span>{{ data.title }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单类型" prop="menuType" :rules="[{ required: true, message: '菜单类型不能为空', trigger: 'blur' }]">
							<el-radio-group v-model="state.ruleForm.menuType" >
								<el-radio v-for="d in props.dictData" :key="d.value" :label="d.value" >{{d.name}}</el-radio>
							</el-radio-group> 
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="菜单名称" prop="title" :rules="[{ required: true, message: '菜单名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.title" placeholder="菜单名称" clearable />
						</el-form-item>
					</el-col>
					<template v-if="state.ruleForm.menuType === 'menu' || state.ruleForm.menuType === 'dirctory'">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="路由名称">
								<el-input v-model="state.ruleForm.name" placeholder="路由名称" clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="路由路径">
								<el-input v-model="state.ruleForm.path" placeholder="路由路径" clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="组件路径">
								<el-input v-model="state.ruleForm.component" placeholder="组件路径" clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="菜单图标">
								<IconSelector v-model="state.ruleForm.icon" placeholder="菜单图标" type="all" />
							</el-form-item>
						</el-col>
						<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="重定向">
								<el-input v-model="state.ruleForm.redirect" placeholder="重定向地址" clearable />
							</el-form-item>
						</el-col> -->
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="链接地址">
								<el-input v-model="state.ruleForm.outLink" placeholder="外链/内嵌时链接地址" clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="菜单排序">
								<el-input-number v-model="state.ruleForm.sortCode" placeholder="排序" class="w100" />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否隐藏">
								<el-radio-group v-model="state.ruleForm.isHide">
									<el-radio :label="true">隐藏</el-radio>
									<el-radio :label="false">不隐藏</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否缓存">
								<el-radio-group v-model="state.ruleForm.isKeepAlive">
									<el-radio :label="true">缓存</el-radio>
									<el-radio :label="false">不缓存</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>												
					</template>
					<template v-if="state.ruleForm.menuType === 'btn'">		
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="按钮权限">
								<el-input v-model="state.ruleForm.path" placeholder="按钮权限" clearable />
							</el-form-item>
						</el-col>				
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="菜单排序">
								<el-input-number v-model="state.ruleForm.sortCode" placeholder="排序" class="w100" />
							</el-form-item>
						</el-col>
					</template>
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">{{ state.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditMenu">
import { reactive, ref } from 'vue';
import IconSelector from '/@/components/iconSelector/index.vue';
import { feature } from '/@/utils/axios-utils';
import { SysMenu, MenuUpdateInput, SysDictData } from '/@/api-services/models/system';
import { ElMessage } from 'element-plus';
import menuApi from '/@/api-services/apis/system/menuApi';
import userCenterApi from '/@/api-services/apis/system/userCenterApi';
const emits = defineEmits(['refresh']);
const props = defineProps({		
	dictData: Array<SysDictData>
});

const ruleFormRef = ref();
const state = reactive({
	title: '',
	submitTxt:'',
	isShowDialog: false,
	type:'',
	ruleForm: {} as MenuUpdateInput,
	menuData: [] as Array<SysMenu>,	
});

// 打开弹窗
const openDialog = async (type:string,row: any) => {
	state.type=type
	if (type === 'edit') {		
		state.title = '修改字典类型';
		state.submitTxt = '修 改';
		state.ruleForm = JSON.parse(JSON.stringify(row));
	} else {
		state.title = '新增字典类型';
		state.submitTxt = '新 增';
		state.ruleForm={} as MenuUpdateInput,
		// 新增的时候，默认上级为选中行
		state.ruleForm.parentId=row.id 
	}
	var res=await userCenterApi.loginUserMenu();
	var menuList= res.data.data;
	menuList.unshift({id:0,parentId:-1,title:'顶层'})
	state.menuData=menuList
	
	state.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		var err:Error |null
		if (state.type === 'add') {
			[err]=await feature(menuApi.add(state.ruleForm));
		}
		else{
			[err]=await feature(menuApi.update(state.ruleForm));
		}
		if(err){
			ElMessage.error(err.message)
		}
		else{
			emits('refresh');
			closeDialog();
		}
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
